Android Studio 简要实现蓝牙(Bluetooth)通信(附加作业) |
您所在的位置:网站首页 › android studio编写程序的步骤 › Android Studio 简要实现蓝牙(Bluetooth)通信(附加作业) |
文章目录
一、项目实现功能二、项目核心代码1.简要实现设备蓝牙通信2.模拟Client 和Server端实现简单的通信。
三、实验项目截图四、源代码
一、项目实现功能
1.两台设备可以通过蓝牙进行通信 2.模拟Client 和Server端实现简单的通信。 二、项目核心代码 1.简要实现设备蓝牙通信如果想让应用启动设备发现或操纵蓝牙设置,则除了 BLUETOOTH 权限以外,还必须声明 BLUETOOTH_ADMIN 权限。大多数应用只是需利用此权限发现本地蓝牙设备。除非应用是根据用户请求修改蓝牙设置的“超级管理员”,否则不应使用此权限所授予的其他功能。在Manifest.xml中加入以下代码 在这里插入代码片 ... ``布局文件: activity.xml主要布局 device_list.xm文件 ChatService.java文件,蓝牙服务的会话程序 定义了3个内部类,AcceptThread(接受新连接)、ConnectThread(发出连接)和ConnectedThread (已连接) package com.example.bluetooth; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; public class ChatService { //本应用的主Activity组件名称 private static final String NAME = "BluetoothChat"; // UUID:通用唯一识别码,是一个128位长的数字,一般用十六进制表示 //算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成 //在创建蓝牙连接 private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); private final BluetoothAdapter adapter; private final Handler mHandler; private AcceptThread acceptThread; private ConnectThread connectThread; private ConnectedThread connectedThread; private int state; public static final int STATE_NONE = 0; public static final int STATE_LISTEN = 1; public static final int STATE_CONNECTING = 2; public static final int STATE_CONNECTED = 3; //构造方法,接收UI主线程传递的对象 public ChatService(Context context, Handler handler) { //构造方法完成蓝牙对象的创建 adapter = BluetoothAdapter.getDefaultAdapter(); state = STATE_NONE; mHandler = handler; } private synchronized void setState(int state) { state = state; mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget(); } public synchronized int getState() { return state; } public synchronized void start() { if (connectThread != null) { connectThread.cancel(); connectThread = null; } if (connectedThread != null) { connectedThread.cancel(); connectedThread = null; } if (acceptThread == null) { acceptThread = new AcceptThread(); acceptThread.start(); } setState(STATE_LISTEN); } //取消 CONNECTING 和 CONNECTED 状态下的相关线程,然后运行新的 connectThread 线程 public synchronized void connect(BluetoothDevice device) { if (state == STATE_CONNECTING) { if (connectThread != null) { connectThread.cancel(); connectThread = null; } } if (connectedThread != null) { connectedThread.cancel(); connectedThread = null; } connectThread = new ConnectThread(device); connectThread.start(); setState(STATE_CONNECTING); } /* 开启一个 ConnectedThread 来管理对应的当前连接。之前先取消任意现存的 connectThread 、 connectedThread 、 acceptThread 线程,然后开启新 connectedThread ,传入当前刚刚接受的 socket 连接。最后通过 Handler来通知UI连接 */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { if (connectThread != null) { connectThread.cancel(); connectThread = null; } if (connectedThread != null) { connectedThread.cancel(); connectedThread = null; } if (acceptThread != null) { acceptThread.cancel(); acceptThread = null; } connectedThread = new ConnectedThread(socket); connectedThread.start(); Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(BluetoothChat.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); } // 停止所有相关线程,设当前状态为 NONE public synchronized void stop() { if (connectThread != null) { connectThread.cancel(); connectThread = null; } if (connectedThread != null) { connectedThread.cancel(); connectedThread = null; } if (acceptThread != null) { acceptThread.cancel(); acceptThread = null; } setState(STATE_NONE); } // 在 STATE_CONNECTED 状态下,调用 connectedThread 里的 write 方法,写入 byte public void write(byte[] out) { ConnectedThread r; synchronized (this) { if (state != STATE_CONNECTED) return; r = connectedThread; } r.write(out); } // 连接失败的时候处理,通知 ui ,并设为 STATE_LISTEN 状态 private void connectionFailed() { setState(STATE_LISTEN); Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST); Bundle bundle = new Bundle(); bundle.putString(BluetoothChat.TOAST, "链接不到设备"); msg.setData(bundle); mHandler.sendMessage(msg); } // 当连接失去的时候,设为 STATE_LISTEN 状态并通知 ui private void connectionLost() { setState(STATE_LISTEN); Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST); Bundle bundle = new Bundle(); bundle.putString(BluetoothChat.TOAST, "设备链接中断"); msg.setData(bundle); mHandler.sendMessage(msg); } // 创建监听线程,准备接受新连接。使用阻塞方式,调用 BluetoothServerSocket.accept() private class AcceptThread extends Thread { private final BluetoothServerSocket mmServerSocket; public AcceptThread() { BluetoothServerSocket tmp = null; try { //使用射频端口(RF comm)监听 tmp = adapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); } catch (IOException e) { } mmServerSocket = tmp; } @Override public void run() { setName("AcceptThread"); BluetoothSocket socket = null; while (state != STATE_CONNECTED) { try { socket = mmServerSocket.accept(); } catch (IOException e) { break; } if (socket != null) { synchronized (ChatService.this) { switch (state) { case STATE_LISTEN: case STATE_CONNECTING: connected(socket, socket.getRemoteDevice()); break; case STATE_NONE: case STATE_CONNECTED: try { socket.close(); } catch (IOException e) { e.printStackTrace(); } break; } } } } } public void cancel() { try { mmServerSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } /* 连接线程,专门用来对外发出连接对方蓝牙的请求和处理流程。 构造函数里通过 BluetoothDevice.createRfcommSocketToServiceRecord() , 从待连接的 device 产生 BluetoothSocket. 然后在 run 方法中 connect , 成功后调用 BluetoothChatSevice 的 connected() 方法。定义 cancel() 在关闭线程时能够关闭相关socket 。 */ private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; public ConnectThread(BluetoothDevice device) { mmDevice = device; BluetoothSocket tmp = null; try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { e.printStackTrace(); } mmSocket = tmp; } @Override public void run() { setName("ConnectThread"); adapter.cancelDiscovery(); try { mmSocket.connect(); } catch (IOException e) { connectionFailed(); try { mmSocket.close(); } catch (IOException e2) { e.printStackTrace(); } ChatService.this.start(); return; } synchronized (ChatService.this) { connectThread = null; } connected(mmSocket, mmDevice); } public void cancel() { try { mmSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } /* 双方蓝牙连接后一直运行的线程;构造函数中设置输入输出流。 run()方法中使用阻塞模式的 InputStream.read()循环读取输入流,然后发送到 UI 线程中更新聊天消息。 本线程也提供了 write() 将聊天消息写入输出流传输至对方,传输成功后回写入 UI 线程。最后使用cancel()关闭连接的 socket */ private class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket) { mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } mmInStream = tmpIn; mmOutStream = tmpOut; } @Override public void run() { byte[] buffer = new byte[1024]; int bytes; while (true) { try { bytes = mmInStream.read(buffer); mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); } catch (IOException e) { connectionLost(); break; } } } public void write(byte[] buffer) { try { mmOutStream.write(buffer); mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); } catch (IOException e) { e.printStackTrace(); } } public void cancel() { try { mmSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }DeviceList.java文件 本程序供菜单项主界面的选项菜单“我的友好”调用,用于: (1)显示已配对的好友列表; (2)搜索可配对的好友进行配对 (3)新选择并配对的蓝牙设备将刷新好友列表 注意:发现新的蓝牙设备并请求配对时,需要对应接受 关键技术:动态注册一个广播接收者,处理蓝牙设备扫描的结果 package com.example.bluetooth; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.provider.Settings; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Set; public class DeviceList extends AppCompatActivity { private BluetoothAdapter adapter; private ArrayAdapter adapter1; // 配对的设备 private ArrayAdapter adapter2; // 新设备 public static String EXTRA_DEVICE_ADDRESS = "device_address"; //Mac地址 //定义广播接收者,用于处理扫描蓝牙设备后的结果 private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { adapter2.add(device.getName() + "\n" + device.getAddress()); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { if (adapter2.getCount() == 0) { String noDevices = getResources().getText(R.string.none_found).toString(); adapter2.add(noDevices); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.device_list); //在被调用活动里,设置返回结果码 setResult(Activity.RESULT_CANCELED); init(); //活动界面 } private void init() { Button scanButton = findViewById(R.id.button_scan); scanButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(DeviceList.this, R.string.scanning, Toast.LENGTH_LONG).show(); doDiscovery(); //搜索蓝牙设备 } }); adapter1 = new ArrayAdapter(this, R.layout.activity_main); adapter2 = new ArrayAdapter(this, R.layout.activity_main); //已配对蓝牙设备列表 ListView pairedListView =findViewById(R.id.paired_devices); pairedListView.setAdapter(adapter1); pairedListView.setOnItemClickListener(mPaireDeviceClickListener); //未配对蓝牙设备列表 ListView newDevicesListView = findViewById(R.id.new_devices); newDevicesListView.setAdapter(adapter2); newDevicesListView.setOnItemClickListener(mNewDeviceClickListener); //动态注册广播接收者 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(receiver, filter); adapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = adapter.getBondedDevices(); if (pairedDevices.size() > 0) { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); for (BluetoothDevice device : pairedDevices) { adapter1.add(device.getName() + "\n" + device.getAddress()); } } else { String noDevices = getResources().getText(R.string.none_paired).toString(); adapter1.add(noDevices); } } @Override protected void onDestroy() { super.onDestroy(); if (adapter != null) { adapter.cancelDiscovery(); } this.unregisterReceiver(receiver); } private void doDiscovery() { findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE); if (adapter.isDiscovering()) { adapter.cancelDiscovery(); } adapter.startDiscovery(); //开始搜索蓝牙设备并产生广播 //startDiscovery是一个异步方法 //找到一个设备时就发送一个BluetoothDevice.ACTION_FOUND的广播 } private AdapterView.OnItemClickListener mPaireDeviceClickListener = new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView av, View v, int arg2, long arg3) { adapter.cancelDiscovery(); String info = ((TextView) v).getText().toString(); String address = info.substring(info.length() - 17); Intent intent = new Intent(); intent.putExtra(EXTRA_DEVICE_ADDRESS, address); //Mac地址 setResult(Activity.RESULT_OK, intent); finish(); } }; private AdapterView.OnItemClickListener mNewDeviceClickListener = new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView av, View v, int arg2, long arg3) { adapter.cancelDiscovery(); Toast.makeText(DeviceList.this, "请在蓝牙设置界面手动连接设备",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); startActivityForResult(intent,1); } }; //回调方法:进入蓝牙配对设置界面返回后执行 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); init(); //刷新好友列表 } } 2.模拟Client 和Server端实现简单的通信。Client端 布局文件 button用以开关蓝牙以及搜索设备的布局按钮 mainactivity.java package com.example.gwlblu; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import android.widget.ToggleButton; import androidx.appcompat.app.AppCompatActivity; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class MainActivity extends AppCompatActivity { //蓝牙通信需要相同的UUID和对方的蓝牙地址,UUID规定是下面的格式,只要格式对,两边的UUID相同,数字可以改变,不影响通信,但一般都是用下面这种 static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; Button btnSearch, btnDis;//定义布局中的按钮 ToggleButton tbtnSwitch;//显示蓝牙开关状态的双状态按钮 ListView lvBTDevices; //搜索到的蓝牙列表 ArrayAdapter adtDevices; //将本机的蓝牙地址显示 List lstDevices = new ArrayList();//列表中蓝牙的地址 BluetoothAdapter btAdapt; //定义移动设备的本地的蓝牙适配器 public static BluetoothSocket btSocket; //Socket用来接受客户端的要求 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //加载 布局 // Button 设置 通过findViewById的方法来定义 //获得所有控件对象 btnSearch = (Button) this.findViewById(R.id.button2); btnDis = (Button) this.findViewById(R.id.button3); tbtnSwitch = (ToggleButton) this.findViewById(R.id.bu5); //给所有的控件设置监听器 btnDis.setOnClickListener(new ClickEvent()); btnSearch.setOnClickListener(new ClickEvent()); tbtnSwitch.setOnClickListener(new ClickEvent()); // ListView及其数据源 适配器 lvBTDevices = (ListView) this.findViewById(R.id.lis1); adtDevices = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, lstDevices); lvBTDevices.setAdapter(adtDevices); lvBTDevices.setOnItemClickListener(new ItemClickEvent());//设置监听器获取数据 btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能 if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 读取蓝牙状态并显示于双状态按钮 tbtnSwitch.setChecked(false); else if (btAdapt.getState() == BluetoothAdapter.STATE_ON) tbtnSwitch.setChecked(true); // 注册Receiver来获取蓝牙设备相关的结果,onReceive()里取得搜索所得的蓝牙设备信息 IntentFilter intent = new IntentFilter(); intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果 intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(searchDevices, intent); } private BroadcastReceiver searchDevices = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Bundle b = intent.getExtras(); Object[] lstName = b.keySet().toArray(); // 显示所有收到的消息及其细节 for (int i = 0; i < lstName.length; i++) { String keyName = lstName[i].toString(); Log.e(keyName, String.valueOf(b.get(keyName))); } //搜索设备时,取得设备的MAC地址 if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String str= device.getName() + "|" + device.getAddress(); if (lstDevices.indexOf(str) == -1)// 防止重复添加, lstDevices.add(str); // 获取设备名称和mac地址 adtDevices.notifyDataSetChanged();//通知Activity刷新数据 } } }; //本次活动的销毁函数 @Override protected void onDestroy() { try { if (btSocket != null) btSocket.close(); } catch (IOException e) { e.printStackTrace(); } this.unregisterReceiver(searchDevices); super.onDestroy(); android.os.Process.killProcess(android.os.Process.myPid()); } //对监听器的设置,获取列表中设备名以及蓝牙设备地址 class ItemClickEvent implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { btAdapt.cancelDiscovery();//连接时停止搜索周围蓝牙,否则容易连接失败 //取出蓝牙地址 String str = lstDevices.get(arg2); String[] values = str.split("\\|"); String address=values[1]; Log.d("address",values[1]); UUID uuid = UUID.fromString(SPP_UUID); //利用BluetoothDevice衍生出Socket, BluetoothDevice btDev = btAdapt.getRemoteDevice(address); try { btSocket = btDev .createRfcommSocketToServiceRecord(uuid); try { // 连接建立之前的先配对 if (btDev.getBondState() == BluetoothDevice.BOND_NONE) { Method creMethod = BluetoothDevice.class .getMethod("createBond"); Log.e("TAG", "开始配对"); creMethod.invoke(btDev); } } catch (Exception e) { e.printStackTrace(); } btSocket.connect(); Toast.makeText(MainActivity.this,"connect succeeded",Toast.LENGTH_SHORT).show(); //将数据写入输出流 OutputStream os = btSocket.getOutputStream(); if (os != null) { try { os.write("test".getBytes("UTF-8")); Toast.makeText(MainActivity.this, "send succeed", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); Toast.makeText(MainActivity.this,"connect fail",Toast.LENGTH_LONG).show(); } } } //按钮监听器,打开本机蓝牙的设置 class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { if (v == btnSearch)// 搜索蓝牙设备,在BroadcastReceiver显示结果 { if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {// 如果蓝牙还没开启 Toast.makeText(MainActivity.this, "请先打开蓝牙", Toast.LENGTH_LONG).show(); return; } setTitle("本机蓝牙地址:" + btAdapt.getAddress()); lstDevices.clear(); btAdapt.startDiscovery(); } else if (v == tbtnSwitch) {// 本机蓝牙启动/关闭 if (tbtnSwitch.isChecked() == false) btAdapt.enable(); else if (tbtnSwitch.isChecked() == true) btAdapt.disable(); } else if (v == btnDis)// 本机可以被搜索 { Intent discoverableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent);//本机蓝牙的内部设置 } } } }Server端 package com.example.gwlblu; import androidx.appcompat.app.AppCompatActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.Toast; import java.io.InputStream; import java.util.UUID; public class Server extends AppCompatActivity { private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");//Server端和Client端的UUID要一致 private BluetoothAdapter bluetoothAdapter; private final String NAME = "BlueTooth_Socket";//名字可以随便写 private AcceptThread acceptThread;//后面的accept()会阻塞,所以要新开线程 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_server); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); acceptThread=new AcceptThread(); acceptThread.start();//开启线程 } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Toast.makeText(Server.this, String.valueOf(msg.obj), Toast.LENGTH_SHORT).show(); } }; private class AcceptThread extends Thread { private BluetoothServerSocket serverSocket; private BluetoothSocket socket; private InputStream is; public AcceptThread() { try { //监听有无连接 serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); } catch (Exception e) { } } @Override public void run() { try { socket = serverSocket.accept();//若有监听到有连接,accept给BluetoothSocket Log.d("tag", "connected"); is = socket.getInputStream(); while (true) { byte[] buffer = new byte[128]; int count = is.read(buffer); //子线程里不能直接Toast,利用handler Message msg = new Message(); msg.obj = new String(buffer, 0, count, "UTF-8"); handler.sendMessage(msg); } } catch (Exception e) { } } } } 三、实验项目截图实验1(真机调试): 简单实现蓝牙通信聊天 gitee源代码1 gitee源代码2 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |